Imports System.IO
Public Class FxStream
    Private _FileStream As System.IO.FileStream
    Private _Reader As System.IO.BinaryReader
    Public Sub New(ByVal aFileName As String)
        _FileStream = New System.IO.FileStream(aFileName, IO.FileMode.Open, IO.FileAccess.Read, IO.FileShare.Read)
        _Reader = New BinaryReader(_FileStream)
    End Sub
    Public Function GetOffset() As Long
        Return _FileStream.Position
    End Function

    Public Function ReadByte() As Byte
        Return _Reader.ReadByte
    End Function
    Public Function ReadWord() As UInt16
        Return _Reader.ReadUInt16
    End Function
    Public Function ReadDWord() As UInt32
        Return _Reader.ReadUInt32
    End Function
    Public Function ReadQWord() As UInt64
        Return _Reader.ReadUInt64
    End Function
    Public Function ReadBytes(ByVal aCount As Integer) As Byte()
        Return _Reader.ReadBytes(aCount)
    End Function
    Public Function ReadBytesWithoutForward(ByVal aCount As Integer) As Byte()
        Dim iLastPosition As Long
        Dim iBytes() As Byte
        iLastPosition = Me.GetOffset()

        iBytes = Me.ReadBytes(aCount)
        Me.Seek(iLastPosition)
        Return iBytes
    End Function
    Public Sub Seek(ByVal aPosition As Long)
        _FileStream.Seek(aPosition, SeekOrigin.Begin)
    End Sub
    Public Function SkipBytes(ByVal aCount As Integer) As Boolean
        If _FileStream.Seek(aCount, SeekOrigin.Current) > 0 Then
            Return True
        End If
        Return False
    End Function
    Public Function ReadStringTillNull(ByRef aStr As String) As Integer
        Dim iChar As Char
        Dim iStrBuilder As New System.Text.StringBuilder
        iChar = _Reader.ReadChar()
        While iChar <> Chr(0)
            iStrBuilder.Append(iChar)
            iChar = _Reader.ReadChar()
        End While
        aStr.Insert(0, iStrBuilder.ToString())
        Return aStr.Length
    End Function
    Public Shared Function ConvertStringToByteArray(ByRef iStr As String) As Byte()
        Dim iSubStr As String
        Dim iLen As Integer = iStr.Length
        Dim iCount As Integer
        Dim iBytes() As Byte
        If iLen Mod 2 = 1 Then
            Throw New Exception("Error while Converting String to byte : " + iStr)
        End If
        iCount = 0
        ReDim iBytes(iLen / 2 - 1)
        While iCount < iLen
            iSubStr = iStr.Substring(iCount, 2)
            iCount = iCount + 2
            iBytes((iCount / 2) - 1) = [Convert].ToByte(iSubStr, 16)
        End While
        Return iBytes
    End Function
   
End Class
